package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.GeneratePageService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApAuthor;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class GeneratePageServiceImpl  implements GeneratePageService {
   @Autowired
    Configuration configuration;
    @Resource(name = "minIOFileStorageService")
    private FileStorageService fileStorageService;
    @Value("${file.minio.prefix}")
    private String prefix;
    @Autowired
    ApArticleMapper apArticleMapper;
    @Autowired
    AuthorMapper authorMapper;
    /**
     * 生成文章静态页
     * @param content
     * @param apArticle
     */
    @Override
    public void generateArticlePage(String content, ApArticle apArticle) {
        //1:获取文章内容

        try {
            //2:模板
            Template template = configuration.getTemplate("article.ftl");
            //3:数据
            Map<String,Object> params=new HashMap<>();
            params.put("content", JSONArray.parseArray(content)); //文本详情
            params.put("article",apArticle);  //文本信息
            ApAuthor apAuthor = authorMapper.selectById(apArticle.getAuthorId());
            params.put("apAuthorApUserId",apAuthor.getUserId()); //作者对应的apUserId
            StringWriter out = new StringWriter();
            template.process(params,out);
            InputStream stream = new ByteArrayInputStream(out.toString().getBytes());
            //4:生成页面把html文件上传到minio中
            String path=fileStorageService.store(prefix,apArticle.getId()+".html","text/html",stream);
            //5:修改ap_article表, 保存static_url字段
            apArticle.setStaticUrl(path);
            apArticleMapper.updateById(apArticle);
            log.info("文章详情动态页面生成成功 staticUrl ======>{}",path);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
            log.error("文件详情静态页面生成失败--------->articleId : {}   -----------{}",apArticle.getId(),e.getCause() );
        }
    }
}
